<script setup lang="ts" name="InfraCronJobDetail">
import { DICT_TYPE } from '@/enums/dictEnums'
import { formatDate } from '@/utils/dateUtil'
import { CronJobApi, CronJobType } from '@/api/infra/cronJob'

const { t } = useI18n() // 国际化
const visible = ref(false) // 弹窗的是否展示
const detailLoading = ref(false) // 表单的加载中
const detailData = ref({} as CronJobType) // 详情数据
const nextTimes = ref([]) // 下一轮执行时间的数组

/** 打开弹窗 */
const open = async (id: number) => {
  visible.value = true
  // 查看，设置数据
  if (id) {
    detailLoading.value = true
    try {
      detailData.value = await CronJobApi.detail(id)
      // 获取下一次执行时间
      nextTimes.value = await CronJobApi.nextTimes(id)
    } finally {
      detailLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>

<template>
  <Dialog v-model="visible" :title="t('infra.cronJob.action', 'detail')" height="500px" type="scroll" width="800px">
    <el-descriptions :column="1" border>
      <el-descriptions-item :label="t('common', 'id')" min-width="120">
        {{ detailData.id }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.cronJob', 'name')">
        {{ detailData.name }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.cronJob', 'status')">
        <dict-tag :type="DICT_TYPE.INFRA_CRON_JOB_STATUS" :value="detailData.status" />
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.cronJob', 'handlerName')">
        {{ detailData.handlerName }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.cronJob', 'handlerParam')">
        {{ detailData.handlerParam }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.cronJob', 'cronExpression')">
        {{ detailData.cronExpression }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.cronJob', 'retryCount')">
        {{ detailData.retryCount }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.cronJob', 'retryInterval')">
        {{ t('infra.cronJob', 'ms', [detailData.retryInterval])  }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.cronJob', 'monitorTimeout')">
        {{ detailData.monitorTimeout > 0 ? t('infra.cronJob', 'ms', [detailData.monitorTimeout]) : t('infra.cronJob', 'unOn') }}
      </el-descriptions-item>
      <el-descriptions-item :label="t('infra.cronJob', 'nextTimes')">
        <el-timeline>
          <el-timeline-item v-for="(nextTime, index) in nextTimes" :key="index" :timestamp="formatDate(nextTime)">{{ t('infra.cronJob', 'times', [(index + 1)]) }}</el-timeline-item>
        </el-timeline>
      </el-descriptions-item>
    </el-descriptions>
  </Dialog>
</template>

<style scoped lang="scss"></style>
